<template>
  <!-- 最外层容器 -->
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/2.jpg" alt="" />
        <span>阿利电商后台管理</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 下方的容器包括 左侧菜单和中间主体 -->
    <el-container>
      <!-- 左侧菜单部分 -->
      <el-aside :width="iscollapse ? '64px' : '240px'">
        <!-- 水平折叠左侧菜单 -->
        <div class="toggle-button" @click="toggleMenu">|||</div>
        <!-- 菜单 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="iscollapse"
          router
          :collapse-transition="false"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文字 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
            <!-- 二级菜单 -->
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 放置一个路由占位符来展示welcome组件 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-mima',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMuneList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取API列表的方法
    async getMuneList() {
      // munus是接口地址
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      // 这里的  $message  是利用 elment-ui 中的组件弹出提示给用户
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击折叠展开左侧菜单
    toggleMenu() {
      this.iscollapse = !this.iscollapse
    },
    // 点击二级菜单将二级菜单的链接保存到sessionStorage中
    saveNavState(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
  span {
    margin-left: 15px;
  }
  img {
    height: 50px;
    width: 50px;
    border-radius: 50%;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  font-size: 16px;
  margin-right: 10px;
}
.toggle-button {
  font-size: 10px;
  background-color: #4a5064;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
